package com.itheima.edu.info.manager.utils;

import com.itheima.edu.info.manager.domain.Person;

import java.io.*;
import java.util.ArrayList;

public class FileUtils {
    private FileUtils() {
    }

    //------------------------------------------------字符流----------------------------------------------------------------------------------

    /**
     * @param list 要写入的参数
     * @param file 写入的路径
     *  将list集合通过字符流写入file路径
     */
    public static <E> void reloadByFileWriter(ArrayList<? extends Person> list, File file) {
        try {
            FileWriter fr = new FileWriter(file);
            for (int i = 0; i < list.size(); i++) {
                fr.write(list.get(i).toString());
                fr.write(System.lineSeparator());
            }
            fr.close();
        } catch (IOException e) {
            System.out.println("路径错误");
        }
    }

    /**
     * @param file 读取的文件的路径
     * @return list 返回的结果集
     *      将file中的文件以字符流的形式读取到list集合中并返回
     */
    public static ArrayList<? extends Person> reSaveByFileReader(File file) {
        ArrayList<Person> list = new ArrayList<>();
        try {
            FileReader fr = new FileReader(file);
            StringBuilder sb = new StringBuilder();
            int b;
            while ((b = fr.read()) != -1) {
                sb.append((char) b);
            }
            fr.close();
            String objArrStr = sb.toString();
            String[] objArr = objArrStr.split(System.lineSeparator());
            for (int i = 0; i < objArr.length; i++) {
                String[] obj = objArr[i].split("\t");
                /*for (int j = 0; j < obj.length; j++) {
                    System.out.println("obj[j]="+obj[j]);
                }*/
                list.add(new Person(obj[0], obj[1], obj[2], obj[3]));
            }


        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
        return list;
    }

    //-----------------------------------------------------字符缓冲流------------------------------------------------------------------------

    /**
     * @param list 要写入的参数
     * @param file 写入的路径
     *   将list集合通过字符流写入file路径
     */
    public static void reloadByBufferedWriter(ArrayList<? extends Person> list, File file) {
        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter(file));
            for (Person person : list) {
                bw.write(person.toString());
                bw.newLine();
            }
            bw.close();
        } catch (IOException e) {
            System.out.println("路径错误");
        }
    }

    /**
     * @param file 读取的文件的路径
     * @return list 返回的结果集
     *       将file中的文件以字符流的形式读取到list集合中并返回
     */
    public static ArrayList<? extends Person> reSaveByBufferedReader(File file) {
        ArrayList<Person> list = new ArrayList<>();
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));
            String objStr;
            while ((objStr = br.readLine()) != null) {
                //System.out.println(objStr);
                String[] obj = objStr.split("\t");
                /*for (int j = 0; j < obj.length; j++) {
                    System.out.println("obj[j]="+obj[j]);
                }*/
                list.add(new Person(obj[0], obj[1], obj[2], obj[3]));
            }


        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
        return list;
    }

    //-----------------------------------------------------序列化流1------------------------------------------------------------------------

    /**
     * @param list 要写入的参数
     * @param file 写入的路径
     *       将list集合中的元素通过序列化流写入file路径
     */
    public static void reloadByObjectWriter(ArrayList<? extends Person> list, File file) {
        try {
            //创建序列化流
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
            for (int i = 0; i < list.size(); i++) {
                oos.writeObject(list.get(i));
            }
            oos.close();
        } catch (IOException e) {
            System.out.println("路径错误");
        }
    }

    /**
     * @param file 读取的文件的路径
     * @return list 返回的结果集
     *       将file中的文件以反序列流的形式读取到list集合中并返回
     */
    public static ArrayList<? extends Person> reSaveByObjectReader(File file) {
        ArrayList<Person> list = new ArrayList<>();
        try {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
            while (true) {
                try {
                    Person person = (Person) ois.readObject();
                    list.add(person);
                } catch (ClassNotFoundException e) {
                    break;
                }
            }
            ois.close();

        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
        return list;
    }

    //-----------------------------------------------------序列化流2------------------------------------------------------------------------

    /**
     * @param list 要写入的参数
     * @param file 写入的路径
     *  将list集合通过序列化流写入file路径
     *
     */
    public static void reloadByObjectWriterList(ArrayList<? extends Person> list, File file) {
        try {
            //创建序列化流
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
            oos.writeObject(list);
            oos.close();
        } catch (IOException e) {
            System.out.println("路径错误");
        }
    }

    /**
     * @param file 读取的文件的路径
     * @return list 返回的结果集
     *  将file中的文件以反序列流的形式读取到list集合中并返回
     */
    public static ArrayList<? extends Person> reSaveByObjectReaderList(File file) {
        ArrayList<Person> list = new ArrayList<>();
        try {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
            try {
                list = (ArrayList<Person>) ois.readObject();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            ois.close();

        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
        return list;
    }

    //-----------------------------------------------------字节缓冲流------------------------------------------------------------------------

    /**
     * @param file 读取的文件的路径
     * @return list 返回的结果集
     *       将file中的文件以字节缓冲流的形式读取到list集合中并返回
     */
    public static ArrayList<? extends Person> reloadByBufferedInputStream(File file) {
        ArrayList<Person> list = new ArrayList<>();
        try {
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
            //定义结果集
            StringBuilder temp = new StringBuilder();
            //定义中间量
            byte[] chars = new byte[1024];
            int len;
            try {
                if (((len = bis.read(chars)) != -1)) {
                    temp.append(new String(chars, 0, len));
                    String s = new String(chars, 0, len);
                    //System.out.println(s);
                }
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            String objArrStr = temp.toString();
            String[] objArr = objArrStr.split(System.lineSeparator());
            for (int i = 0; i < objArr.length; i++) {
                String[] obj = objArr[i].split("\t");
                /*for (int j = 0; j < obj.length; j++) {
                    System.out.println("obj[i] = " + obj[j]);
                }*/
                list.add(new Person(obj[0], obj[1], obj[2], obj[3]));
            }


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * @param list 要写入的参数
     * @param file 写入的路径
     *       将list集合通过字节缓冲流写入file路径
     */
    public static void reloadByBufferedOutputStream(ArrayList<? extends Person> list, File file) {
        try {
            BufferedOutputStream bis = new BufferedOutputStream(new FileOutputStream(file));
            for (int i = 0; i < list.size(); i++) {
                bis.write(list.get(i).toString().getBytes());
                bis.write(System.lineSeparator().getBytes());
            }
            bis.close();
        } catch (IOException e) {
            System.out.println("路径错误");
        }
    }

    //-----------------------------------------------------字节流------------------------------------------------------------------------

    /**
     * @param file 读取的文件的路径
     * @return list 返回的结果集
     *       将file中的文件以字节缓冲流的形式读取到list集合中并返回
     */
    public static ArrayList<? extends Person> reloadByInputStream(File file) {
        ArrayList<Person> list = new ArrayList<>();
        try {
            FileInputStream fis = new FileInputStream(file);
            //定义结果集
            StringBuilder temp = new StringBuilder();
            //定义中间量
            int bytes;
            try {
                while ((bytes = fis.read()) != -1) {
                    temp.append((char)bytes);
                    //System.out.println(bytes);
                }
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            String objArrStr = temp.toString();
            //System.out.println("objArrStr = " + objArrStr);
            String[] objArr = objArrStr.split(System.lineSeparator());
            for (int i = 0; i < objArr.length; i++) {
                String[] obj = objArr[i].split("\t");
               /* for (int j = 0; j < obj.length; j++) {
                    System.out.println("obj[i] = " + obj[j]);
                }*/
                list.add(new Person(obj[0], obj[1], obj[2], obj[3]));
            }


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * @param list 要写入的参数
     * @param file 写入的路径
     *      将list集合通过字节缓冲流写入file路径
     */
    public static void reloadByFileOutputStream(ArrayList<? extends Person> list, File file) {
        try {
            FileOutputStream bis = new FileOutputStream(file);
            for (int i = 0; i < list.size(); i++) {
                bis.write(list.get(i).toString().getBytes());
                bis.write(System.lineSeparator().getBytes());
            }
            bis.close();
        } catch (IOException e) {
            System.out.println("路径错误");
        }
    }

    //-----------------------------------------------------字符缓冲流&转码流------------------------------------------------------------------------

    /**
     * @param list 要写入的参数
     * @param file 写入的路径
     *  将list集合通过字符流写入file路径
     */
    public static void reloadByOutputStreamWriter(ArrayList<? extends Person> list, File file) {
        try {
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),"GBK"));
            for (Person person : list) {
                bw.write(person.toString());
                bw.newLine();
            }
            bw.close();
        } catch (IOException e) {
            System.out.println("路径错误");
        }
    }

    /**
     * @param file 读取的文件的路径
     * @return list 返回的结果集
     *  将file中的文件以字符流的形式读取到list集合中并返回
     */
    public static ArrayList<? extends Person> reSaveByInputStreamReader(File file) {
        ArrayList<Person> list = new ArrayList<>();
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file),"GBK"));
            String objStr;
            while ((objStr = br.readLine()) != null) {
                //System.out.println(objStr);
                String[] obj = objStr.split("\t");
                /*for (int j = 0; j < obj.length; j++) {
                    System.out.println("obj[j]="+obj[j]);
                }*/
                list.add(new Person(obj[0], obj[1], obj[2], obj[3]));
            }


        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
        return list;
    }



}
